package com.dt.dubai.mall.controller;

import com.dt.dubai.core.comm.PageCondition;
import com.dt.dubai.core.comm.PageData;
import com.dt.dubai.core.comm.PageParameterData;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.core.utile.DateTools;
import com.dt.dubai.mall.controller.innate.MallRetailCommodityInnateAbs;
import com.dt.dubai.mall.entity.MallRetailCommodityEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* 电商零售商品信息表
*/

@Controller
@RequestMapping(value = "/mall/mallRetailCommodity")
public class MallRetailCommodityController extends MallRetailCommodityInnateAbs {


    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MallRetailCommodityEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
    }

    @Override
    protected void userModEntity(MallRetailCommodityEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("adminLoginRecordId");
            map.remove("merchantBrandRecordId");
            map.remove("commodityTypeRecordId");
            map.remove("commodityAtbuRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Short commodityType = null == map.get("commodityType") ? -1 :  (Short) map.get("commodityType");
            switch (commodityType) {
                case 1:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "电商");
                    break;
                case 2:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "检测");
                    break;
                case 3:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "计量");
                    break;
                default:
                    map.put("commodityType", -1);
                    map.put("commodityTypeStr", "未知");
            }
            Short releaseState = null == map.get("releaseState") ? -1 :  (Short) map.get("releaseState");
            switch (releaseState) {
                case 1:
                    map.put("releaseState", releaseState);
                    map.put("releaseStateStr", "下架");
                    break;
                case 2:
                    map.put("releaseState", releaseState);
                    map.put("releaseStateStr", "上架");
                    break;
                case 3:
                    map.put("releaseState", releaseState);
                    map.put("releaseStateStr", "禁售");
                    break;
                default:
                    map.put("releaseState", -1);
                    map.put("releaseStateStr", "未知");
            }
            Boolean newState = null == map.get("newState") ? false : (Boolean) map.get("newState");
            if (newState) {
                map.put("newState", newState);
                map.put("newStateStr", "是");
            } else {
                map.put("newState", newState);
                map.put("newStateStr", "否");
            }
            Boolean recommendState = null == map.get("recommendState") ? false : (Boolean) map.get("recommendState");
            if (recommendState) {
                map.put("recommendState", recommendState);
                map.put("recommendStateStr", "是");
            } else {
                map.put("recommendState", recommendState);
                map.put("recommendStateStr", "否");
            }
            Boolean verificationState = null == map.get("verificationState") ? false : (Boolean) map.get("verificationState");
            if (verificationState) {
                map.put("verificationState", verificationState);
                map.put("verificationStateStr", "是");
            } else {
                map.put("verificationState", verificationState);
                map.put("verificationStateStr", "否");
            }
            Boolean yesPreview = null == map.get("yesPreview") ? false : (Boolean) map.get("yesPreview");
            if (yesPreview) {
                map.put("yesPreview", yesPreview);
                map.put("yesPreviewStr", "是");
            } else {
                map.put("yesPreview", yesPreview);
                map.put("yesPreviewStr", "否");
            }
            Short commodityService = null == map.get("commodityService") ? -1 :  (Short) map.get("commodityService");
            switch (commodityService) {
                case 1:
                    map.put("commodityService", commodityService);
                    map.put("commodityServiceStr", "无忧退货");
                    break;
                case 2:
                    map.put("commodityService", commodityService);
                    map.put("commodityServiceStr", "快速退款");
                    break;
                case 3:
                    map.put("commodityService", commodityService);
                    map.put("commodityServiceStr", "免费包邮");
                    break;
                case 4:
                    map.put("commodityService", commodityService);
                    map.put("commodityServiceStr", "无服务");
                    break;
                default:
                    map.put("commodityService", -1);
                    map.put("commodityServiceStr", "未知");
            }
            Integer examineState = null == map.get("examineState") ? -1 :  (Integer) map.get("examineState");
            switch (examineState) {
                case 1:
                    map.put("examineState", examineState);
                    map.put("examineStateStr", "未提");
                    break;
                case 2:
                    map.put("examineState", examineState);
                    map.put("examineStateStr", "审中");
                    break;
                case 3:
                    map.put("examineState", examineState);
                    map.put("examineStateStr", "通过");
                    break;
                case 4:
                    map.put("examineState", examineState);
                    map.put("examineStateStr", "驳回");
                    break;
                default:
                    map.put("examineState", -1);
                    map.put("examineStateStr", "未知");
            }
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    //修改上架状态
    @RequestMapping(value = "/updateReleaseState", method = RequestMethod.GET)
    @ResponseBody
    public PageData<String> updateReleaseState(Short releaseState,Long recordId){
        try {
            mallRetailCommoditySer.updateReleaseState(releaseState,recordId);
            return new PageData().success("sucess", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

}